home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9152 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: undergrad.math.uwaterloo.ca!sckettle
  3. From: sckettle@undergrad.math.uwaterloo.ca (Steve Kettle)
  4. Subject: Re: How to generate all the combinations of n numbers?
  5. Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
  6. Message-ID: <DnICrA.Dox@undergrad.math.uwaterloo.ca>
  7. Date: Wed, 28 Feb 1996 22:49:58 GMT
  8. References: <4giuc9$pq@news.cis.okstate.edu>
  9. Nntp-Posting-Host: noether.math.uwaterloo.ca
  10. Organization: University of Waterloo
  11.  
  12. In article <4giuc9$pq@news.cis.okstate.edu>,
  13. Madhav Kakani <kakani@osuunx.ucc.okstate.edu> wrote:
  14. >Hello everybody,
  15. >    I am having problems in writing a c/c++ program which
  16. >generates all possible combinations of n given numbers.
  17. >For example, if an array x has some numbers
  18. >int x[] = {1,2,3};
  19. >
  20. >the program should generate the following output:
  21. >1
  22. >2
  23. >3
  24. >12
  25. >13
  26. >23
  27. >123
  28. >
  29. >Thanks in advance,
  30. >                    -madhav
  31.  
  32. I won't give you the answer but recursion is the way to go.  Eg. if you 
  33. know the combinations of n-1 numbers then what can you say about the 
  34. combinations of n numbers.  
  35.  
  36. This is for distinct numbers only - I allowed to repeat the same number 
  37. more than once it prob. gets hairy.
  38. -- 
  39.